package cn.edu.ujn.finallab.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.finallab.util.Page;
import cn.edu.ujn.finallab.dao.Customer;
import cn.edu.ujn.finallab.dao.CustomerMapper;
import cn.edu.ujn.finallab.dao.Orders;
@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService {

	@Autowired
	private CustomerMapper customerMapper;
	
	@Override
	public Customer findById(int id) {
		// TODO Auto-generated method stub
		
		return this.customerMapper.selectByPrimaryKey(id);
	}
	
	//使用分页查询查询所有顾客
	@Override
	public Page<Customer> selectByPage(int currentPage) {
		// TODO Auto-generated method stub
		HashMap<String, Object> map = new HashMap<String, Object>();
		Page<Customer> page = new Page<Customer>();
		// 封装当前页数
		page.setPage(currentPage);
		// 每页显示的数据
		int pageSize = 12;
		page.setSize(pageSize);
		// 封装总记录数
		int totalCount = customerMapper.selectCustomerCount();
		System.out.println("总的记录数为" + totalCount);
		// 获取记录总数
		page.setTotal(totalCount);
		// 封装总页数
		double totalcount = totalCount;
		double num = Math.ceil(totalcount / pageSize);
		page.setTotalpage((int)num);
		// 起始的记录值
		map.put("start", (currentPage - 1) * pageSize);
		// 获取每个页面的pageSize
		map.put("size", page.getSize());
		List<Customer> list = customerMapper.selectByPageLimit(map);
		for(Customer customer:list) {
			String idcard = customer.getIdcard();
			String showIdcard = idcard.substring(0, 6) + "********" + idcard.substring(14, 18);
			customer.setIdcard(showIdcard);
		}
		page.setRows(list);
		// 返回一个page
		return page;
	}
	
	@Override
	public int deleteCustomer(int id) {
		return this.customerMapper.deleteByPrimaryKey(id);
	}
	
	// 修改顾客信息
	@Override
	public int modifyCustomer(Customer customer) {
		return this.customerMapper.updateByPrimaryKey(customer);
	}

	@Override
	public Customer selectByName(String name) {
		// TODO Auto-generated method stub
		return this.customerMapper.selectByName(name);
	}

	@Override
	public int insertCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return this.customerMapper.insert(customer);
	}
	
	@Override
	public Customer selectByIdcard(String idcard) {
		return this.customerMapper.selectByIdcard(idcard);
	}

	@Override
	public List<Orders> selectByCustomerName(String name) {
		// TODO Auto-generated method stub
		return customerMapper.selectByOrderName(name);
	}

}
